package com.mage.completeproject.component;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登陆检查
 */
public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("loginUser");
        if (user == null){
            //未登陆，返回登陆页面
            request.setAttribute("msg","没有权限请先登陆!");
            request.getRequestDispatcher("/index.html").forward(request,response);//请求转发到index.html-->login.html
            //response.sendRedirect("index.html"); //重定向到/index.html-->login.html,但是重定向会清除request的值此处的msg不会保存下来，所以不行
            return false;
        }else {
            //已登陆，放行请求
            return true;
        }
    }
}
